package com.linedata.acq.metier.norme.fmb;

import java.util.StringTokenizer;

import oracle.forms.jdapi.Block;
import oracle.forms.jdapi.Canvas;
import oracle.forms.jdapi.FormModule;
import oracle.forms.jdapi.Graphics;
import oracle.forms.jdapi.Item;
import oracle.forms.jdapi.JdapiIterator;
import oracle.forms.jdapi.Trigger;

public class ZOOM_FMB {

	private FormModule	fmb;

	public ZOOM_FMB(FormModule fmb) {
		this.fmb = fmb;
	}

	private String	error	= "";

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error += error;
	}

	/**
	 * EKFR010_1 :: Le nom des boutons de zoom est EDIT_ « nom_du_champ »
	 * 
	 * @param fmb
	 */
	public void nomZoom() {

		for (JdapiIterator blocks = fmb.getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();

			for (JdapiIterator items = block.getItems(); items.hasNext();) {
				Item item = (Item) items.next();

				if (item.getItemType() == 7 && item.isSubclassed()
						&& item.getSubclassParent().getName().startsWith("IESO$BUTTON_EDIT")
						&& !item.getName().startsWith("EDIT_")) {

					this.setError("\tEKFR010_1 : " + block.getName() + "\t::  " + item.getName() + "\n");
				}

			}
		}
	}

	/**
	 * EKFR027_1 :: La fenêtre de zoom est alignée en bas et décalée de 5
	 * par rapport à la fenêtre principale
	 * 
	 * @param fmb
	 */
	public void ZoneZoom() {

		String blockNameFinalBlock = "";
		String pageNameFinalBlock = "";
		String blockNameFinalDet = "";
		String pageNameFinalDet = "";
		String blockNameFinalItem = "";
		String pageNameFinalItem = "";

		for (JdapiIterator blocks = fmb.getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();

			for (JdapiIterator items = block.getItems(); items.hasNext();) {
				Item item = (Item) items.next();

				for (JdapiIterator triggers = item.getTriggers(); triggers.hasNext();) {

					Trigger trigger = (Trigger) triggers.next();
					if (trigger.getName().toUpperCase().equals("KEY-EDIT")) {

						if (trigger.getTriggerText().toUpperCase().contains("GO_BLOCK")) {

							StringTokenizer ligneToken = new StringTokenizer(trigger.getTriggerText(), "\n");
							while (ligneToken.hasMoreTokens()) {

								String ligne = ligneToken.nextToken();

								if (ligne.toUpperCase().contains("SET_WINDOW_PROPERTY")) {

									StringTokenizer pageNameToken = new StringTokenizer(ligne, "\'");
									int i = 0;
									while (pageNameToken.hasMoreTokens()) {

										String pageName = pageNameToken.nextToken();
										i++;
										if (i == 2) {

											pageNameFinalBlock = pageName.toUpperCase();
										}
									}
								}

								if (ligne.toUpperCase().contains("GO_BLOCK")) {

									StringTokenizer blockNameToken = new StringTokenizer(ligne, "\'");
									int j = 0;
									while (blockNameToken.hasMoreTokens()) {

										String blockName = blockNameToken.nextToken();
										j++;
										if (j == 2) {

											blockNameFinalBlock = blockName.toUpperCase();
										}
									}
								}
							}

							for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

								Canvas canvas = (Canvas) canvases.next();
								if (canvas.getName().toUpperCase().equals(pageNameFinalBlock)) {

									for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

										Graphics graphics = (Graphics) graphicses.next();

										if (graphics.getName().toUpperCase().equals("GRPH_" + blockNameFinalBlock)) {

											if (graphics.getYPosition() + graphics.getHeight() != canvas.getHeight()
													|| graphics.getXPosition() + graphics.getWidth() != canvas
															.getWidth() + 5) {

												this.setError("\tEKFR027_1 : " + canvas.getName() + "\t::  "
														+ graphics.getName() + "\n");
											}
										}
									}
								}
							}
						}

						if (trigger.getTriggerText().toUpperCase().contains("DET$TRAIT_DETAIL")) {

							StringTokenizer ligneToken = new StringTokenizer(trigger.getTriggerText(), "\n");
							while (ligneToken.hasMoreTokens()) {

								String ligne = ligneToken.nextToken();

								if (ligne.toUpperCase().contains("SET_WINDOW_PROPERTY")) {

									StringTokenizer pageNameToken = new StringTokenizer(ligne, "\'");
									int i = 0;
									while (pageNameToken.hasMoreTokens()) {

										String pageName = pageNameToken.nextToken();
										i++;
										if (i == 2) {

											pageNameFinalDet = pageName.toUpperCase();
										}
									}
								}

								if (ligne.toUpperCase().contains("DET$TRAIT_DETAIL")) {

									StringTokenizer blockNameToken = new StringTokenizer(ligne, "\'");
									int j = 0;
									while (blockNameToken.hasMoreTokens()) {

										String blockName = blockNameToken.nextToken();
										j++;
										if (j == 2) {

											blockNameFinalDet = blockName.toUpperCase();
										}
									}
								}
							}

							for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

								Canvas canvas = (Canvas) canvases.next();
								if (canvas.getName().toUpperCase().equals(pageNameFinalDet)) {

									for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

										Graphics graphics = (Graphics) graphicses.next();

										if (graphics.getName().toUpperCase().equals("GRPH_" + blockNameFinalDet)) {

											if (graphics.getYPosition() + graphics.getHeight() != canvas.getHeight()
													|| graphics.getXPosition() + graphics.getWidth() != canvas
															.getWidth() + 5) {

												this.setError("\tEKFR027_1 : " + canvas.getName() + "\t::  "
														+ graphics.getName() + "\n");
											}
										}
									}
								}
							}
						}

						if (trigger.getTriggerText().toUpperCase().contains("GO_ITEM")) {

							StringTokenizer ligneToken = new StringTokenizer(trigger.getTriggerText(), "\n");
							while (ligneToken.hasMoreTokens()) {

								String ligne = ligneToken.nextToken();

								if (ligne.toUpperCase().contains("SET_WINDOW_PROPERTY")) {

									StringTokenizer pageNameToken = new StringTokenizer(ligne, "\'");
									int i = 0;
									while (pageNameToken.hasMoreTokens()) {

										String pageName = pageNameToken.nextToken();
										i++;
										if (i == 2) {

											pageNameFinalItem = pageName.toUpperCase();
										}
									}
								}

								if (ligne.toUpperCase().contains("GO_ITEM")) {

									StringTokenizer blockNameToken = new StringTokenizer(ligne, "\'");
									int j = 0;
									while (blockNameToken.hasMoreTokens()) {

										String blockName = blockNameToken.nextToken();
										j++;
										if (j == 2) {

											if (blockName.contains(".")) {

												blockNameFinalItem = blockName.toUpperCase().substring(0,
														blockName.indexOf("."));
											} else {

												blockNameFinalItem = blockName.toUpperCase();
											}
										}
									}
								}
							}

							for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

								Canvas canvas = (Canvas) canvases.next();
								if (canvas.getName().toUpperCase().equals(pageNameFinalItem)) {

									for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

										Graphics graphics = (Graphics) graphicses.next();

										if (graphics.getName().toUpperCase().equals("GRPH_" + blockNameFinalItem)) {

											if (graphics.getYPosition() + graphics.getHeight() != canvas.getHeight()
													|| graphics.getXPosition() + graphics.getWidth() != canvas
															.getWidth() + 5) {

												this.setError("\tEKFR027_1 : " + canvas.getName() + "\t::  "
														+ graphics.getName() + "\n");
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	@Override
	public String toString() {
		return this.getError();
	}
}
